package com.lanou.myapp.comic.rankingcomic;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.lanou.myapp.R;
import com.lanou.myapp.greendao.RankingComic;
import com.lanou.myapp.imageloader.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/12.
 */
public class RankingComicAdapter extends BaseAdapter{
    private List<RankingComic> data;
    private Context context;
    private ImageLoader imageLoader;
    private RequestQueue queue;
    private LruCache<String, Bitmap> lruCache;
    private ImageLoader.ImageCache imageCache;

    public RankingComicAdapter(List<RankingComic> data, Context context) {
        this.data = data;
        this.context = context;
//        init();
    }


//    private void init() {
//        queue = Volley.newRequestQueue(context);
//        lruCache  = new LruCache<String, Bitmap>(20);
//        imageCache = new ImageLoader.ImageCache() {
//            @Override
//            public Bitmap getBitmap(String url) {
//                return lruCache.get(url);
//            }
//
//            @Override
//            public void putBitmap(String url, Bitmap bitmap) {
//                lruCache.put(url, bitmap);
//            }
//        };
//
//        imageLoader = new ImageLoader(queue, imageCache);
//    }




    @Override
    public int getCount() {
        return data.size() > 0 && data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_ranking_comic,null);
            myViewHolder = new MyViewHolder();
            myViewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_item_ranking_comic);
            myViewHolder.tv_authors = (TextView) convertView.findViewById(R.id.tv_name_ranking_comic);
            myViewHolder.tv_type = (TextView) convertView.findViewById(R.id.tv_type_ranking_comic);
            myViewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time_ranking_comic);
            myViewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_item_ranking_comic);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }


//        String imgUrl = data.get(position).getCover();
//        getBitmapCache(imgUrl, myViewHolder.imageView);
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imageView, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
        VolleySingleton.getImageLoader().get(data.get(position).getCover(), listener, 400, 300);
//        Glide.with(context).load(data.get(position).getCover()).into(myViewHolder.imageView);


        myViewHolder.tv_title.setText(data.get(position).getTitle());
        myViewHolder.tv_authors.setText(data.get(position).getAuthors());
        myViewHolder.tv_type.setText(data.get(position).getTypes());
        myViewHolder.tv_time.setText(String.valueOf(data.get(position).getLast_updatetime()));

        return convertView;
    }


//    public void getBitmapCache(String imgUrl, ImageView imageView) {
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.v6_boy_unseletor_icon,
//                R.mipmap.v6_boy_unseletor_icon);
//        imageLoader.get(imgUrl, listener, 280, 250);
//    }


    class MyViewHolder{
        TextView tv_title, tv_authors, tv_type, tv_time;
        ImageView imageView;

    }

}
